//
//  NavigationController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/23.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "NavigationController.h"
#import "RESideMenu.h"
#import "SetViewController.h"
#import "SoftwareDetailsController.h"
#import "offlineController.h"

#import "UMSocial.h"
//#import "UMSocialDataService.h"

@interface NavigationController ()
@property (nonatomic, strong) RESideMenu *sideMenu;
@end

@implementation NavigationController

- (void)popSelf
{
    [self popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:YES];
    
    if (viewController.navigationItem.leftBarButtonItem == nil && [self.viewControllers count] > 1)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 25, 25);
        [button addTarget:self action:@selector(popSelf) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc]initWithCustomView:button];
        viewController.navigationItem.leftBarButtonItem = backBarBtn;

    }
    else
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 25, 25);
        [button addTarget:self action:@selector(popLeft) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:button];
        viewController.navigationItem.leftBarButtonItem = leftBarBtn;
        
        UIButton *share = [UIButton buttonWithType:UIButtonTypeCustom];
        [share setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        share.frame = CGRectMake(0, 0, 25, 25);
        [share addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:share];
        viewController.navigationItem.rightBarButtonItem = rightBarBtn;
        
        
    }
    viewController.navigationController.navigationBar.translucent = NO;
}

- (void)share:(UIButton *)btn
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56596ac867e58e4a2e0001ef"
                                      shareText:@"全面的旅行攻略 让你的旅行更简单 https://itunes.apple.com/cn/app/FreeTrip/id1063905261?mt=8 来自FreeTrip"
                                     shareImage:[UIImage imageNamed:@"free trip"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToQQ, UMShareToQzone,  UMShareToWechatTimeline, UMShareToWechatSession, UMShareToEmail, UMShareToSms,nil]
                                       delegate:nil];
    
    
    
}


- (void)popLeft
{
    if (!_sideMenu)
    {
        RESideMenuItem *setItem = [[RESideMenuItem alloc]initWithTitle:@"设置" action:^(RESideMenu *menu, RESideMenuItem *item) {
            [menu hide];
            SetViewController *setViewVc = [[SetViewController alloc]init];
            UINavigationController *setNav = [[UINavigationController alloc]initWithRootViewController:setViewVc];
            [menu setRootViewController:setNav];

        }];
        RESideMenuItem *softwareDetailItem = [[RESideMenuItem alloc]initWithTitle:@"软件说明" action:^(RESideMenu *menu, RESideMenuItem *item) {
            [menu hide];
            SoftwareDetailsController *softVc = [[SoftwareDetailsController alloc]init];
            UINavigationController *softNav = [[UINavigationController alloc]initWithRootViewController:softVc];
            [menu setRootViewController:softNav];
        }];
        RESideMenuItem *offlineItem = [[RESideMenuItem alloc]initWithTitle:@"离线使用说明" action:^(RESideMenu *menu, RESideMenuItem *item) {
            [menu hide];
            offlineController *offlineVc = [[offlineController alloc]init];
            UINavigationController *offNav = [[UINavigationController alloc]initWithRootViewController:offlineVc];
            [menu setRootViewController:offNav];
        }];
        _sideMenu = [[RESideMenu alloc]initWithItems:@[setItem, softwareDetailItem, offlineItem]];
    }
    [_sideMenu show];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
